using Godot;
using System;
using CurrentGame.Utils.Interpolation;

namespace CurrentGame.Placements;


public partial class LightMemory : Node2D
{

	private Player _player;
	private Sprite2D _sprite;
	private LerpInterpolation _interpolation;
	private bool _disappearing = false;

	public override void _Ready()
	{
		var map = GetNode<NodeReferenceMap>(Constants.NodeRefMapPath);
		_player = map.MainPlayer;
		_sprite = GetNode<Sprite2D>("Sprite2D");

		_interpolation = new LerpInterpolation(0.016f, new Vector3(0.001f, -1f, -1f))
		{
			Value = 1.0f,
			Weight = 0.1f
		};
		_interpolation.Teleport();
        _interpolation.ReachStableValue += Signal_ReachStableValue;
	}

    public override void _Process(double delta)
    {
        if (_interpolation.Process(delta))
		{
			Modulate = new Color(1.0f, 1.0f, 1.0f, _interpolation.Value);
			_sprite.SetInstanceShaderParameter("Strength", _interpolation.Value);
		}
    }

	public void HideMem()
	{
		if (!_disappearing)
		{
			_interpolation.Value = 0.0f;
			_disappearing = true;
		}
	}
	
	private void Signal_PlayerEnterLight(Area2D area)
	{
		_player.NotifyLightMemoryUpdate(this);
	}

	private void Signal_PlayerLeaveLight(Area2D area)
	{
		_player.NotifyLightMemoryUpdate(null);
	}

    private void Signal_ReachStableValue(float v, Vector2 v2, Vector3 v3)
    {
		if (_disappearing)
		{
			Visible = false;
			QueueFree();
		}
    }
}
